<script lang="ts" setup>
import { ref ,onMounted} from "vue";

onMounted(()=>{
    console.log(this);
})
// --深拷贝
const checkObj = (obj: any) => {
  if (typeof obj !== "object" || obj === null) return false;
  return true;
};

const cloneDeep = (obj: any) => {
  if (!checkObj(obj)) return obj;
  let target: any = Array.isArray(obj) ? [] : {};
  for (let key in obj) {
    target[key] = checkObj(obj[key]) ? cloneDeep(obj[key]) : obj[key];
  }
  return target;
};

let obj = { a: 1, b: { c: 2, d: 3 }, e: 4 };
let obj4 = Object.assign({}, obj);
obj4.b.c = 10;
let obj3 = JSON.parse(JSON.stringify(obj));
obj3.b.c = 10;
obj3.e = 90;
console.log("obj3", obj3);
let obj2 = cloneDeep(obj);
console.log("obj2", obj2);
obj2.a = 15;
console.log("obj", obj);

// --js高阶函数

function foo(n: number) {
  return n + 1;
}

function count(funnc: Function) {
  let count = 0;
  return [
    (...args: any[]) => {
      console.log("args", args);
      funnc(...args);
      count++;
    },
    () => {
      console.log("count", count);
    },
  ];
}
let [countFoo, getCount] = count(foo);
countFoo(1);
countFoo(1);
countFoo(1);
countFoo(1);
countFoo(1);
getCount();

function greet(name: string, callback: Function) {
  console.log(`Hello ${name}`);
  callback();
}

function sayGoodbye() {
  console.log("Goodbye!");
}
greet("John", sayGoodbye);

// --只执行一次的函数
function once(fn: Function) {
    console.log(arguments)
  let bool = false;
  console.log(bool)
  return function (this: any,...args: any[]) {
    console.log("args", args);
    if(!bool){
        bool = true;
        console.log(this)
       return fn.apply(this, args);
    }
  };
}
let pay=once(function(money:number){
console.log(`You paid ${money} yuan`);
});
pay(5);
pay(5);
pay(5);



const showArgs=(...args:any[])=>{
    console.log("args",args);
}
showArgs('凌风','陈冬梅','小妲己');

</script>

<template>
  <div></div>
</template>
